#include <ftp_server.h>
#include <user/user_manager.h>
#include <inicpp.h>
#include <memory_allocator.h>

int main() {
//#ifndef TEST_BUDDY_ALLOCATOR
//	std::vector<MemoryChunk> chunks;
//	for(int i=0;i<16;++i){
//		chunks.push_back(MemoryAllocator::instance().allocate(2048*(1<<i)));
//	}
//	
//	for(int i=0;i<16;++i){
//        MemoryAllocator::instance().deallocate(chunks[i]);
//	}
//
//	for(int i=0;i<16;++i){
//        chunks[i] = (MemoryAllocator::instance().allocate(2048 * (1 << i)));
//	}
//#endif

#if _WIN32
	// VS默认设置utf-8编码
	setlocale(LC_ALL, ".utf-8");
	SetConsoleOutputCP(CP_UTF8);
#endif

#ifdef JLFTP_ENABLE_OPENSSL
    std::cout << "jlftp-server enable openssl!" << std::endl;
#endif // JLFTP_ENABLE_OPENSSL

	// 加载server配置
	ini::IniFile serverIni("./resource/server.ini");
	assert(serverIni.find("server") != serverIni.end());
	auto& serverSection = serverIni["server"];
	assert(serverSection.find("ip") != serverSection.end() && serverSection.find("port") != serverSection.end());
	std::string serverIP = serverSection["ip"].as<std::string>();
	std::uint16_t serverPort = serverSection["port"].as<std::uint16_t>();
	// ssl部分
	std::string sslcert = serverSection["cert"].as<std::string>();
	std::string sslkey = serverSection["key"].as<std::string>();

	// 加载user信息
	ini::IniFile userIni("resource/users.ini");
	for (auto& userPair : userIni) {
		auto& user = userPair.second;
		std::string username = user["username"].as<std::string>();
		std::string password = user["password"].as<std::string>();
		int permission = user["permission"].as<int>();
		std::string root = user["root"].as<std::string>();
		std::shared_ptr<FTPUser> ftpUser = std::make_shared<FTPUser>(username, password, permission, root);
		UserManager::instance().addUser(ftpUser);
	}

	// 启动ftp服务
	FTPServer server(serverIP, serverPort);
	server.enableSSL(sslcert, sslkey);
	server.start();
}